<template>
  <el-dialog
    :modelValue="visible"
    @update:modelValue="$emit('update:visible', $event)"
    class="report_dialog"
    width="40%"
  >
    <div class="title">收藏规则</div>
    <el-form ref="menuRef" :model="form" :rules="rules" label-width="auto" label-position="top">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="部门" prop="station">
            <el-select v-model="form.station" placeholder="请选择">
              <el-option
                v-for="itemf in stationList"
                :key="itemf.value"
                :label="itemf.stationName"
                :value="itemf.stationName"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则名称" prop="title">
            <el-input v-model.trim="form.title" placeholder="请输入规则名称" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="handleClose">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { onBeforeMount, onMounted, reactive, ref } from 'vue'
import { ElMessage, useDisabled } from 'element-plus'
import { addRuleCollectAPI, getStationAPI } from '@/api/ruleKind.js'
import { useAuthStore } from '@/stores/auth.js'
const authStore = useAuthStore()
const emit = defineEmits(['update:visible', 'changeRule'])
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  ruleObj: {
    type: Object,
    default: () => {},
  },
})
const form = ref<any>({})
const rules = ref({
  title: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
  station: [{ required: true, message: '请选择部门', trigger: 'change' }],
})

const { proxy } = getCurrentInstance()
const stationList = ref([])

const handleClose = () => {
  emit('update:visible', false)
  proxy.$refs['menuRef'].resetFields()
  form.value = {}
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['menuRef'].validate((valid) => {
    if (valid) {
      let submitData = {
        file: props.ruleObj.file,
        rule: {
          title: form.value.title,
          station: form.value.station,
          ruleContent: props.ruleObj.ruleContent,
        },
      }
      addRuleCollectAPI(submitData).then((res) => {
        // console.log(res, ';resres')
        if (res.code === 200) {
          ElMessage.success('添加成功！')
          emit('update:visible', false)
          emit('changeRule', { title: form.value.title, station: form.value.station })
        }
      })
      console.log(submitData, ';validvalid')
    }
  })
}
onBeforeMount(() => {})
onMounted(() => {
  form.value.title = props.ruleObj.title
  form.value.station = props.ruleObj.station
  getStationAPI().then((res) => {
    if (res.code === 200) {
      stationList.value = res.data
    }
  })
})
defineExpose({})
</script>
<style lang="scss" scoped>
.report_dialog {
  .title {
    text-align: center;
    font-weight: 800;
    font-size: 18px;
    margin-bottom: 20px;
  }
}
</style>
